/*
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */
package org.geoscheduler.android.tools;

import org.geoscheduler.android.R;
import org.geoscheduler.android.activities.ListEvents;
import org.geoscheduler.android.activities.Login;
import org.geoscheduler.android.activities.MapOverview;
import org.geoscheduler.android.activities.ServicesStarter;
import org.geoscheduler.android.activities.Setup;

import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

/**
 * Helper that makes it easy to build consistent context menus across application.
 * 
 * It has prefix "GS", because there is already MenuBuilder in android.widget.
 *
 * @author srakyi
 */
public class GSMenuBuilder {
	
	public static enum Item {
		LOGIN(R.string.login_menuitem, Login.class, R.drawable.menu_login),
		SETUP(R.string.setup_menuitem, Setup.class, R.drawable.menu_setup),
		LIST(R.string.le_menuitem, ListEvents.class, R.drawable.menu_list),
		SERVICES_START(R.string.services_menuitem_start, ServicesStarter.class, R.drawable.menu_services_start, ServicesStarter.class.getName() + ServicesStarter.PAR_START, true),
		SERVICES_STOP(R.string.services_menuitem_stop, ServicesStarter.class, R.drawable.menu_services_stop, ServicesStarter.class.getName() + ServicesStarter.PAR_START, false),
		SHOW_MAPOVERVIEW(R.string.mo_menuitem, MapOverview.class, R.drawable.world);
		
		private int titleId;
		private Class<?> clazz;
		private int iconId;
		
		private boolean hasExtra = false;
		private String extraName;
		private Boolean extraValue;
		
		Item(int titleId, Class<?> clazz, int iconId) {
			this.titleId = titleId;
			this.clazz = clazz;
			this.iconId = iconId;
		}
		
		Item(int titleId, Class<?> clazz, int iconId, String extraName, Boolean extraValue) {
			this(titleId, clazz, iconId);
			
			this.hasExtra = true;
			this.extraName = extraName;
			this.extraValue = extraValue;
		}
		
		public CharSequence getTitle(Context ctx) {
			return ctx.getText(this.titleId);
		}
		
		public Class<?> getClazz() {
			return this.clazz;
		}

		public int getIconId() {
			return iconId;
		}

		public boolean hasExtra() {
			return hasExtra;
		}

		public String getExtraName() {
			return extraName;
		}

		public Boolean getExtraValue() {
			return extraValue;
		}
	}
	
	public static boolean createOptionsMenu(Menu menu, Context ctx, Item... items) {
		for (Item m : items) {
			menu.add(0, m.ordinal(), m.ordinal(), m.getTitle(ctx)).setIcon(m.getIconId());
		}

		return true;
	}
	
	public static boolean menuItemSelected(Context ctx, MenuItem item) {
		Item i = Item.values()[item.getItemId()];
		
		Intent intent = new Intent(ctx, i.getClazz());
		if (i.hasExtra) {
			intent.putExtra(i.getExtraName(), i.getExtraValue());
		}
		
		ctx.startActivity(intent);
		
		return true;
	}
}
